package com.demo.common.ratelimit.utils;

import java.util.function.Supplier;

/**
 * 睡醒阻塞
 *
 * @author DayDayUp
 * create on 2023-08-25
 **/
public class SleepUtil {

    public static boolean untilGetTrue(long millis, Supplier<Boolean> supplier) throws InterruptedException {
        int i = 0;
        int count = (int) (millis / 100);
        do {
            boolean tryLock2 = supplier.get();
            if (tryLock2) {
                return true;
            }
            Thread.sleep(100);
            i++;
        } while (i < count);
        return false;
    }

    public static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

}
